package com.backend.demo.tag;

import java.util.List;

import javax.servlet.jsp.JspWriter;

import org.springframework.web.servlet.tags.RequestContextAwareTag;

import com.backend.demo.dao.EnumItemDao;
import com.backend.demo.dao.EnumSortDao;
import com.backend.demo.entities.EnumItem;
import com.backend.demo.entities.EnumSort;
import com.backend.demo.util.StringUtils;

/**
 * 自定义下拉标签
 * 
 * @author Administrator
 * 
 */
public class SelectTag extends RequestContextAwareTag {

	private static final long serialVersionUID = 1L;

	private EnumItemDao enumItemDao;
	
	private EnumSortDao enumSortDao;

	private String enumWord;

	private String cssClass;

	private String cssId;

	private String attrName;

	private String placeholder;
	
	private String language;
	
	

	public String getLanguage() {
		return language;
	}

	public void setLanguage(String language) {
		this.language = language;
	}

	public String getEnumWord() {
		return enumWord;
	}

	public void setEnumWord(String enumWord) {
		this.enumWord = enumWord;
	}

	public String getCssClass() {
		return cssClass;
	}

	public void setCssClass(String cssClass) {
		this.cssClass = cssClass;
	}

	public String getCssId() {
		return cssId;
	}

	public void setCssId(String cssId) {
		this.cssId = cssId;
	}

	public String getAttrName() {
		return attrName;
	}

	public void setAttrName(String attrName) {
		this.attrName = attrName;
	}

	public String getPlaceholder() {
		return placeholder;
	}

	public void setPlaceholder(String placeholder) {
		this.placeholder = placeholder;
	}

	@Override
	protected int doStartTagInternal() throws Exception {
		enumItemDao = (EnumItemDao) this.getRequestContext().getWebApplicationContext().getBean("enumItemDao");
		enumSortDao = (EnumSortDao) this.getRequestContext().getWebApplicationContext().getBean("enumSortDao");
		JspWriter out = pageContext.getOut();
		StringBuffer stringBuffer = new StringBuffer("<select name='" + attrName+"'");
		if (StringUtils.isNotEmpty(cssClass)) {
			stringBuffer.append("class='" + cssClass+"'");
		}
		if (StringUtils.isNotEmpty(cssId)) {
			stringBuffer.append("id='" + cssId+"'");
		}
		if (StringUtils.isNotEmpty(placeholder)) {
			stringBuffer.append("placeholder='" + placeholder+"'");
		}
		if("zh_CN".equals(language)){
			stringBuffer.append("><option value=-1>--请选择--</option>");
		}else{
			stringBuffer.append("><option value=-1>--please select one--</option>");
		}
		List<EnumSort> enumSorts = enumSortDao.getAllEnumSorts();
		if(StringUtils.isEmpty(enumWord)){
			if(enumSorts != null && enumSorts.size() > 0){
				for (EnumSort enumSort : enumSorts) {
					if("zh_CN".equals(language)){
						stringBuffer.append("<option value='" + enumSort.getEnumsortid() + "'>"
								+ enumSort.getName() + "</option>");
					}else{
 						stringBuffer.append("<option value='" + enumSort.getEnumsortid() + "'>"
								+ enumSort.getNameen() + "</option>");
					}
				}
			}
		}else{
			List<EnumItem> enumItems = enumItemDao.getAllEnumItemsByEnumWord(enumWord);
			if (enumItems != null && enumItems.size() > 0) {
				for (EnumItem enumItem : enumItems) {
					if("zh_CN".equals(language)){
						stringBuffer.append("<option value='" + enumItem.getCode() + "'>"
								+ enumItem.getName() + "</option>");
					}else{
						stringBuffer.append("<option value='" + enumItem.getCode() + "'>"
								+ enumItem.getNameen() + "</option>");
					}
				}
			}
		}
		stringBuffer.append("</select>");
		out.print(stringBuffer.toString());
		return 0;
	}
}
